package com.pansoft.openplanet.adapter;

import android.app.AlertDialog;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.chat.utils.LXGlideImageLoader;
import com.efounder.util.ToastUtil;
import com.efounder.utils.JfResourceUtil;
import com.efounder.utils.ResStringUtil;
import com.efounder.view.CircleImageView;
import com.pansoft.kline.model.KlineOptionalOrMVModel;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.activity.AssetListActivity;
import com.pansoft.openplanet.activity.BarcodeReceiveActivity;
import com.pansoft.openplanet.activity.EnergyQRCodeActivity;
import com.pansoft.openplanet.activity.IPAssetsWalletDetailActivity;
import com.pansoft.openplanet.activity.NewTransferActivity;
import com.pansoft.openplanet.activity.SafetyArchitectureActivity;
import com.pansoft.openplanet.activity.TradeLineActivity;
import com.pansoft.openplanet.activity.TransferActivity;
import com.pansoft.openplanet.bean.Account;
import com.pansoft.openplanet.bean.AssetListBean;
import com.pansoft.openplanet.constant.IPAssetsConstant;
import com.pansoft.openplanet.db.AccountDao;
import com.pansoft.openplanet.util.EosNetParamUtil;
import com.utilcode.util.ToastUtils;

import java.util.ArrayList;

import pl.droidsonroids.gif.AnimationListener;
import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

/**
 * 子钱包列表
 */
public class ChildWalletListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int VIEW_TYPE_HEADER = 0;
    private static final int VIEW_TYPE_BODY = 1;
    private static final int VIEW_TYPE_FOOTER = 2;

    private Context mContext;
    private ArrayList<AssetListBean> orderList;
    private int mode;
    //top 显示余额
    private String balance;
    //行情数据
    private KlineOptionalOrMVModel klineOptionalOrMVModel;

    public ChildWalletListAdapter(Context mContext, ArrayList<AssetListBean> orderList, int mode) {
        this.mContext = mContext;
        this.orderList = orderList;
        this.mode = mode;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == VIEW_TYPE_HEADER) {
            view = LayoutInflater.from(mContext).inflate(R.layout.open_planet_item_wallet_header, parent, false);
            return new WalletHeaderHolder(view);
        } else if (viewType == VIEW_TYPE_BODY) {
            view = LayoutInflater.from(mContext).inflate(R.layout.open_planet_item_wallet_asset, parent, false);
            return new WalletAssetHolder(view);
        } else if (viewType == VIEW_TYPE_FOOTER) {
//            view = LayoutInflater.from(mContext).inflate(R.layout.layout_order_item_footer, parent, false);
//            return new OrderFooterHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof WalletHeaderHolder) {
            //头部view 以及数据
            AccountDao accountDao = new AccountDao(mContext);
            Account account = new Account();

            if (balance == null) {
                balance = "0";
            }
            balance = balance.replace(" BTC", "");
            ((WalletHeaderHolder) holder).tvNum.setText(balance);
            if (mode == IPAssetsConstant.CHAIN_ID_ETH) {
                account = accountDao.getCurrentShowAccount(IPAssetsConstant.ChainEnum.ETH);
                if (account == null) {
                    account = accountDao.getMainAccount();
                }
                ((WalletHeaderHolder) holder).tvTitle.setText(account.getAcccountName());
                ((WalletHeaderHolder) holder).ivAddAsset.setVisibility(View.VISIBLE);

                ((WalletHeaderHolder) holder).ivCoin.setImageDrawable(mContext.getResources()
                        .getDrawable(R.drawable.eth));
                ((WalletHeaderHolder) holder).ivCoin.setVisibility(View.VISIBLE);
                ((WalletHeaderHolder) holder).ivPWRCoin.setVisibility(View.GONE);
                if (account.getAccountAvatar() == null || account.getAccountAvatar().equals("")) {
                    LXGlideImageLoader.getInstance().displayRoundCornerImage(mContext, ((WalletHeaderHolder) holder).ivWallet,
                            R.drawable.star_asset_default_wallet_icon, R.drawable.star_asset_default_wallet_icon, R.drawable.star_asset_default_wallet_icon, 20);
                } else {
                    LXGlideImageLoader.getInstance().displayRoundCornerImage(mContext, ((WalletHeaderHolder) holder).ivWallet,
                            account.getAccountAvatar(), R.drawable.star_asset_default_wallet_icon, R.drawable.star_asset_default_wallet_icon, 20);
                }



                //一键发币
                ((WalletHeaderHolder) holder).btnOneClickSend.setVisibility(View.GONE);
                ((WalletHeaderHolder) holder).btnOneClickSend.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new AlertDialog.Builder(mContext).setTitle(ResStringUtil.getString(R.string.open_planet_asset_mine_pwr_one_click_coin))
                                .setMessage(ResStringUtil.getString(R.string.open_planet_recharge_no_dredge))
                                .setCancelable(true)
                                .setPositiveButton(ResStringUtil.getString(R.string.common_text_confirm), null).show();
                    }
                });
            } else if (mode == IPAssetsConstant.CHAIN_ID_ETE) {
                account = accountDao.getCurrentShowAccount(IPAssetsConstant.ChainEnum.ETE);
                if (account == null) {
                    account = accountDao.getMainAccount();
                }
                ((WalletHeaderHolder) holder).tvTitle.setText(account.getAcccountName());
                ((WalletHeaderHolder) holder).ivAddAsset.setVisibility(View.VISIBLE);

                ((WalletHeaderHolder) holder).ivCoin.setImageDrawable(mContext.getResources()
                        .getDrawable(R.drawable.ete2));
                ((WalletHeaderHolder) holder).ivCoin.setVisibility(View.VISIBLE);
                ((WalletHeaderHolder) holder).ivPWRCoin.setVisibility(View.GONE);
                if (account.getAccountAvatar() == null || account.getAccountAvatar().equals("")) {
                    LXGlideImageLoader.getInstance().displayRoundCornerImage(mContext, ((WalletHeaderHolder) holder).ivWallet,
                            R.drawable.star_asset_default_wallet_icon, R.drawable.star_asset_default_wallet_icon, R.drawable.star_asset_default_wallet_icon, 20);
                } else {
                    LXGlideImageLoader.getInstance().displayRoundCornerImage(mContext, ((WalletHeaderHolder) holder).ivWallet,
                            account.getAccountAvatar(), R.drawable.star_asset_default_wallet_icon, R.drawable.star_asset_default_wallet_icon, 20);
                }



                //一键发币
                ((WalletHeaderHolder) holder).btnOneClickSend.setVisibility(View.GONE);
                ((WalletHeaderHolder) holder).btnOneClickSend.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new AlertDialog.Builder(mContext).setTitle(ResStringUtil.getString(R.string.open_planet_asset_mine_pwr_one_click_coin))
                                .setMessage(ResStringUtil.getString(R.string.open_planet_recharge_no_dredge))
                                .setCancelable(true)
                                .setPositiveButton(ResStringUtil.getString(R.string.common_text_confirm), null).show();
                    }
                });
            } else if (mode == IPAssetsConstant.CHAIN_ID_BTC) {
                account = accountDao.getCurrentShowAccount(IPAssetsConstant.ChainEnum.BTC);
                ((WalletHeaderHolder) holder).tvTitle.setText(account.getAcccountName());
                ((WalletHeaderHolder) holder).ivAddAsset.setVisibility(View.INVISIBLE);
                ((WalletHeaderHolder) holder).ivCoin.setImageDrawable(mContext.getResources()
                        .getDrawable(R.drawable.btc_copy));
                ((WalletHeaderHolder) holder).ivCoin.setVisibility(View.VISIBLE);
                ((WalletHeaderHolder) holder).ivPWRCoin.setVisibility(View.GONE);
                if (account.getAccountAvatar() == null || account.getAccountAvatar().equals("")) {
                    LXGlideImageLoader.getInstance().displayRoundCornerImage(mContext, ((WalletHeaderHolder) holder).ivWallet,
                            R.drawable.star_asset_default_wallet_icon, R.drawable.star_asset_default_wallet_icon, R.drawable.star_asset_default_wallet_icon, 20);
                } else {
                    LXGlideImageLoader.getInstance().displayRoundCornerImage(mContext, ((WalletHeaderHolder) holder).ivWallet,
                            account.getAccountAvatar(), R.drawable.star_asset_default_wallet_icon, R.drawable.star_asset_default_wallet_icon, 20);
                }
            } else if (mode == IPAssetsConstant.CHAIN_ID_TURBOCHAIN) {
                account = accountDao.getMainAccount();
                ((WalletHeaderHolder) holder).tvTitle.setText(mContext.getResources().getString(R.string.open_planet_asset_mine_pwr_energy));
                ((WalletHeaderHolder) holder).ivAddAsset.setVisibility(View.VISIBLE);
                ((WalletHeaderHolder) holder).ivCoin.setImageDrawable(mContext.getResources()
                        .getDrawable(R.drawable.ic_top_pwr));
                ((WalletHeaderHolder) holder).ivWallet.setVisibility(View.GONE);
                ((WalletHeaderHolder) holder).ivPWRCoin.setVisibility(View.VISIBLE);
                ((WalletHeaderHolder) holder).ivPWRCoin.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (((WalletHeaderHolder) holder).isCoinClickable) {
                            ((WalletHeaderHolder) holder).isCoinClickable = false;
                            try {
                                GifDrawable coinToBack = new GifDrawable(mContext.getResources(), R.drawable.me_energy_fronttoback);
                                coinToBack.setLoopCount(1);
                                final GifDrawable coinFront = new GifDrawable(mContext.getResources(), R.drawable.me_energy_front);

                                ((WalletHeaderHolder) holder).ivPWRCoin.setImageDrawable(coinToBack);
                                coinToBack.addAnimationListener(new AnimationListener() {
                                    @Override
                                    public void onAnimationCompleted(int loopNumber) {
                                        try {
                                            GifDrawable coinBack = new GifDrawable(mContext.getResources(), R.drawable.me_energy_back);
                                            coinBack.setLoopCount(1);
                                            ((WalletHeaderHolder) holder).ivPWRCoin.setImageDrawable(coinBack);
                                            coinBack.addAnimationListener(new AnimationListener() {
                                                @Override
                                                public void onAnimationCompleted(int loopNumber) {
                                                    try {
                                                        final GifDrawable coinBackToFront = new GifDrawable(mContext.getResources(), R.drawable.me_energy_backtofront);
                                                        coinBackToFront.setLoopCount(1);
                                                        ((WalletHeaderHolder) holder).ivPWRCoin.setImageDrawable(coinBackToFront);
                                                        coinBackToFront.addAnimationListener(new AnimationListener() {
                                                            @Override
                                                            public void onAnimationCompleted(int loopNumber) {
                                                                ((WalletHeaderHolder) holder).ivPWRCoin.setImageDrawable(coinFront);
                                                                ((WalletHeaderHolder) holder).isCoinClickable = true;
                                                            }
                                                        });
                                                    } catch (Exception e) {
                                                        e.printStackTrace();
                                                    }
                                                }
                                            });
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }

                                    }
                                });
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
            } else if (mode == EosNetParamUtil.getEosChainId()) {
                account = accountDao.getCurrentShowAccount(EosNetParamUtil.getEosChainEnum());
                ((WalletHeaderHolder) holder).tvTitle.setText(account.getAcccountName());
                ((WalletHeaderHolder) holder).ivAddAsset.setVisibility(View.INVISIBLE);
                ((WalletHeaderHolder) holder).ivCoin.setImageDrawable(mContext.getResources()
                        .getDrawable(R.drawable.eosblue));
                ((WalletHeaderHolder) holder).ivCoin.setVisibility(View.VISIBLE);
                ((WalletHeaderHolder) holder).ivPWRCoin.setVisibility(View.GONE);
                if (account.getAccountAvatar() == null || account.getAccountAvatar().equals("")) {
                    LXGlideImageLoader.getInstance().displayRoundCornerImage(mContext, ((WalletHeaderHolder) holder).ivWallet,
                            R.drawable.star_asset_default_wallet_icon, R.drawable.star_asset_default_wallet_icon, R.drawable.star_asset_default_wallet_icon, 20);
                } else {
                    LXGlideImageLoader.getInstance().displayRoundCornerImage(mContext, ((WalletHeaderHolder) holder).ivWallet,
                            account.getAccountAvatar(), R.drawable.star_asset_default_wallet_icon, R.drawable.star_asset_default_wallet_icon, 20);
                }
//                LXGlideImageLoader.getInstance().showRoundUserAvatar(mContext, ((WalletHeaderHolder) holder).ivWallet,
//                        R.drawable.btc_avatar, 20);
            }
            final int accountType = account.getAccountType();
            if (accountType == 0) {
                //正常
                ((WalletHeaderHolder) holder).ivWalletStatus.setImageDrawable(mContext.getResources()
                        .getDrawable(R.drawable.active_wallet));
            } else if (accountType == 1) {
                //观察
                ((WalletHeaderHolder) holder).ivWalletStatus.setImageDrawable(mContext.getResources()
                        .getDrawable(R.drawable.observer_wallet));
            } else if (accountType == 2) {
                ((WalletHeaderHolder) holder).ivWalletStatus.setImageDrawable(mContext.getResources()
                        .getDrawable(R.drawable.frozen_wallet));
            }

            /**
             * 行情数据
             */
            if(klineOptionalOrMVModel!=null){
                ((WalletHeaderHolder) holder).cl_coin_info.setVisibility(View.VISIBLE);
                ((WalletHeaderHolder) holder).tv_title.setText(klineOptionalOrMVModel.getTitle());
                ((WalletHeaderHolder) holder).tv_name.setText(klineOptionalOrMVModel.getName());
                ((WalletHeaderHolder) holder).tv_price.setText(klineOptionalOrMVModel.getPrice());
                ((WalletHeaderHolder) holder).tv_num.setText(klineOptionalOrMVModel.getNum());
                ((WalletHeaderHolder) holder).tv_sub_price.setText(klineOptionalOrMVModel.getSubPrice());
                ((WalletHeaderHolder) holder).btn_change_rate.setText(klineOptionalOrMVModel.getChangeRate());

                if(klineOptionalOrMVModel.isIncrease()){
                    ((WalletHeaderHolder) holder).btn_change_rate.setBackgroundResource(R.drawable.kline_shape_btn_item__change_green_bg);
                }else {
                    ((WalletHeaderHolder) holder).btn_change_rate.setBackgroundResource(R.drawable.kline_shape_btn_item__change_red_bg);
                }
                //稳定
                if(klineOptionalOrMVModel.getTrendType() == 0){
                    ((WalletHeaderHolder) holder).tv_price.setTextColor(JfResourceUtil.getSkinColor(R.color.chart_main_white_or_black));
                    ((WalletHeaderHolder) holder).tv_sub_price.setTextColor(JfResourceUtil.getSkinColor(R.color.black_text_color));
                    //涨
                }else if(klineOptionalOrMVModel.getTrendType() == 1){
                    ((WalletHeaderHolder) holder).tv_price.setTextColor(JfResourceUtil.getSkinColor(R.color.increasing_color));
                    ((WalletHeaderHolder) holder).tv_sub_price.setTextColor(JfResourceUtil.getSkinColor(R.color.increasing_color));
                }else if(klineOptionalOrMVModel.getTrendType() == -1){
                    //跌
                    ((WalletHeaderHolder) holder).tv_price.setTextColor(JfResourceUtil.getSkinColor(R.color.decreasing_color));
                    ((WalletHeaderHolder) holder).tv_sub_price.setTextColor(JfResourceUtil.getSkinColor(R.color.decreasing_color));
                }
            }else {
                ((WalletHeaderHolder) holder).cl_coin_info.setVisibility(View.GONE);
            }
            final String walletName = account.getAcccountName();
            final String address = account.getAddress();
            final String walletIcon = account.getAccountAvatar();

            //更新钱包地址或者头像
            final Account finalAccount = account;
            ((WalletHeaderHolder) holder).tvTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startUpdateAvatar(finalAccount);
                }
            });
            ((WalletHeaderHolder) holder).ivWallet.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startUpdateAvatar(finalAccount);
                }
            });
            if (mode == EosNetParamUtil.getEosChainId() && !account.isActivate()) {
                //钱包未激活
                ((WalletHeaderHolder) holder).tvAddress.setText(mContext.getString(R.string.to_be_activated));
                ((WalletHeaderHolder) holder).tvAddress.setOnClickListener(null);
                ((WalletHeaderHolder) holder).tvList.setOnClickListener(null);
                ((WalletHeaderHolder) holder).tvOut.setOnClickListener(null);
                ((WalletHeaderHolder) holder).tvIn.setOnClickListener(null);
                ((WalletHeaderHolder) holder).ivCopy.setOnClickListener(null);
            } else {
                ((WalletHeaderHolder) holder).tvList.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //如果是PWR
                        if(mode == IPAssetsConstant.CHAIN_ID_TURBOCHAIN){
                            TradeLineActivity.startTradeLineActivity
                                    (mContext, IPAssetsConstant.CHAIN_ID_TURBOCHAIN, ResStringUtil.getString(R.string.open_planet_transfer_list), finalAccount.getAddress(),
                                            "", balance, null,
                                            finalAccount.getFileName(), accountType);
                            return;
                        }
                        //进入交易明细界面
                        TradeLineActivity.startTradeLineActivity
                                (mContext, mode, walletName, address, walletIcon, balance,
                                        null, null, accountType);
                    }
                });
                ((WalletHeaderHolder) holder).tvOut.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (accountType == 1) {
                            ToastUtil.showToast(mContext, ResStringUtil.getString(R.string.open_planet_asset_child_wallet_list_not_transfer));
                            return;
                        }
                        if(mode == IPAssetsConstant.CHAIN_ID_TURBOCHAIN){
                            //转账界面
                            Intent intent = new Intent(mContext, TransferActivity.class);
                            intent.putExtra("myaddress", finalAccount.getAddress());
                            intent.putExtra("balance", balance);
                            intent.putExtra("filePath", finalAccount.getFileName());
                            mContext.startActivity(intent);
                            return;
                        }
                        //普通转账，非Token代币转账
                        NewTransferActivity.start(mContext, walletName, mode, address, balance,
                                null, accountType);
                    }
                });
                ((WalletHeaderHolder) holder).tvIn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mode == IPAssetsConstant.CHAIN_ID_ETH) {
                            BarcodeReceiveActivity.start(mContext, walletName, address, walletIcon,
                                    "ETH", "", mode, "");
                        } else if(mode == IPAssetsConstant.CHAIN_ID_TURBOCHAIN){
                            //转入界面
                            Intent intent = new Intent(mContext, EnergyQRCodeActivity.class);
                            mContext.startActivity(intent);
                        }else if(mode == IPAssetsConstant.CHAIN_ID_ETE){
                            BarcodeReceiveActivity.start(mContext, walletName, address, walletIcon,
                                    "ETE", "", mode, "");
                        } else {
                            BarcodeReceiveActivity.start(mContext, walletName, address, walletIcon,
                                    mode == IPAssetsConstant.CHAIN_ID_BTC ? "BTC" : "EOS", "", mode, "");
                        }

                    }
                });
                if (address.length() > 20) {
                    String formatAddress = address.replace(address.substring(6, address.length() - 4), "****");
                    ((WalletHeaderHolder) holder).tvAddress.setText(formatAddress);
                } else {
                    ((WalletHeaderHolder) holder).tvAddress.setText(address);
                }
                ((WalletHeaderHolder) holder).tvAddress.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        copyAddress(address);
                    }
                });
                ((WalletHeaderHolder) holder).ivCopy.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        copyAddress(address);
                    }
                });
            }


            //点击进入代币列表界面，传入ETH钱包余额，作为第一条展示
            final Account finalAccount1 = account;
            ((WalletHeaderHolder) holder).ivAddAsset.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext, AssetListActivity.class);
                    intent.putExtra("mode",mode);
                    intent.putExtra("asset_list", orderList);
                    intent.putExtra("accountAddress", finalAccount1.getAddress());
                    if (finalAccount1 != null) {
                        mContext.startActivity(intent);
                    }

                }
            });
        } else if (holder instanceof WalletAssetHolder) {
            //代币列表view以及数据
            AccountDao accountDao = new AccountDao(mContext);
            //查询当前显示的账户
            Account account = accountDao.getCurrentShowAccount(IPAssetsConstant.ChainEnum.ETH);
            if (mode == IPAssetsConstant.CHAIN_ID_BTC) {
                account = accountDao.getCurrentShowAccount(IPAssetsConstant.ChainEnum.BTC);
            } else if (mode == EosNetParamUtil.getEosChainId()) {
                account = accountDao.getCurrentShowAccount(EosNetParamUtil.getEosChainEnum());
            } else if (mode == IPAssetsConstant.CHAIN_ID_ETE) {
                account = accountDao.getCurrentShowAccount(IPAssetsConstant.ChainEnum.ETE);
            }
            if (account == null) {
                account = accountDao.getMainAccount();
            }
            final String walletAddress = account.getAddress();
            final String walletName = account.getAcccountName();
            final String walletIcon = account.getAccountAvatar();
            final int accountType = account.getAccountType();
            //第一条是默认的，为ETH钱包
            if (position == 1) {
                if (mode == IPAssetsConstant.CHAIN_ID_BTC) {
                    ((WalletAssetHolder) holder).tvAssetName.setText(mContext.getResources().getString(R.string.BTC_NAME));
                    ((WalletAssetHolder) holder).tvAssetValue.setText("0");
                    LXGlideImageLoader.getInstance().showRoundUserAvatar(mContext, ((WalletAssetHolder) holder).ivWallet,
                            mContext.getResources().getString(R.string.BTC_IMG_URL), 100);

                    ((WalletAssetHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            TradeLineActivity.startTradeLineActivity(mContext, mode,
                                    walletName, walletAddress, walletIcon, balance,
                                    null, null, accountType);
                        }
                    });
                } else if (mode == IPAssetsConstant.CHAIN_ID_ETH) {
                    ((WalletAssetHolder) holder).tvAssetName.setText(mContext.getResources().getString(R.string.ETH_NAME));
                    ((WalletAssetHolder) holder).tvAssetValue.setText(balance);
                    LXGlideImageLoader.getInstance().showRoundUserAvatar(mContext, ((WalletAssetHolder) holder).ivWallet,
                            mContext.getResources().getString(R.string.ETH_IMG_URL), 100);
                    //点击进入钱包详情
                    ((WalletAssetHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            TradeLineActivity.startTradeLineActivity(mContext, mode,
                                    walletName, walletAddress, walletIcon, balance,
                                    null, null, accountType);
                        }
                    });
                    //PWR
                }else if(mode == IPAssetsConstant.CHAIN_ID_TURBOCHAIN) {
                    account = accountDao.getMainAccount();
                    ((WalletAssetHolder) holder).tvAssetName.setText(mContext.getResources().getString(R.string.open_planet_asset_mine_pwr_energy));
                    ((WalletAssetHolder) holder).tvAssetValue.setText(balance);
                    LXGlideImageLoader.getInstance().showRoundUserAvatar(mContext, ((WalletAssetHolder) holder).ivWallet,
                            R.drawable.ic_list_pwr, 10);
                    //点击进入钱包详情
                    final Account finalAccount = account;
                    ((WalletAssetHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            TradeLineActivity.startTradeLineActivity
                                    (mContext, IPAssetsConstant.CHAIN_ID_TURBOCHAIN, ResStringUtil.getString(R.string.open_planet_transfer_list), finalAccount.getAddress(),
                                            "", balance, null,
                                            finalAccount.getFileName(), accountType);
                        }
                    });
                } else if (mode == IPAssetsConstant.CHAIN_ID_ETE) {
                    ((WalletAssetHolder) holder).tvAssetName.setText("ETE");
                    ((WalletAssetHolder) holder).tvAssetValue.setText(balance);
                    LXGlideImageLoader.getInstance().showRoundUserAvatar(mContext, ((WalletAssetHolder) holder).ivWallet,
                            R.drawable.ete2, 100);
                    //点击进入钱包详情
                    ((WalletAssetHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            TradeLineActivity.startTradeLineActivity(mContext, mode,
                                    walletName, walletAddress, walletIcon, balance,
                                    null, null, accountType);
                        }
                    });
                }  else {
                    ((WalletAssetHolder) holder).tvAssetName.setText(EosNetParamUtil.getEosChainName());
                    ((WalletAssetHolder) holder).tvAssetValue.setText(balance);
//            ((WalletAssetHolder) holder).tvAssetExchangeValue.setText((orderList.get(position-1)).getExchangeValue());
                    LXGlideImageLoader.getInstance().showRoundUserAvatar(mContext, ((WalletAssetHolder) holder).ivWallet,
                            mContext.getResources().getString(R.string.EOS_IMG_URL), 100);
                    //点击进入钱包详情
                    ((WalletAssetHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            TradeLineActivity.startTradeLineActivity
                                    (mContext, mode, walletName, walletAddress, walletIcon, balance,
                                            null, null, accountType);
                        }
                    });
                }
            } else {
                //代币列表数据
                ((WalletAssetHolder) holder).tvAssetName.setText((orderList.get(position).getName()));
                ((WalletAssetHolder) holder).tvAssetValue.setText((orderList.get(position)).getBalance());
//            ((WalletAssetHolder) holder).tvAssetExchangeValue.setText((orderList.get(position-1)).getExchangeValue());
                LXGlideImageLoader.getInstance().showRoundUserAvatar(mContext, ((WalletAssetHolder) holder).ivWallet,
                        orderList.get(position).getIcon(), 100);
                //点击进入代币详情
                ((WalletAssetHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TradeLineActivity.startTradeLineActivity(mContext, mode, walletName,
                                walletAddress, walletIcon, balance,
                                orderList.get(holder.getAdapterPosition()), null, accountType);
                    }
                });
            }
        }

    }

    //修改钱包头像或者名称
    private void startUpdateAvatar(Account finalAccount) {
        if(mode == IPAssetsConstant.CHAIN_ID_TURBOCHAIN){
            Intent intent = new Intent(mContext, SafetyArchitectureActivity.class);
            intent.putExtra("filePath", finalAccount.getFileName());
            mContext.startActivity(intent);
            return;
        }
        IPAssetsWalletDetailActivity.start(mContext, finalAccount.getAddress());
    }

    @Override
    public int getItemViewType(int position) {
        if (orderList.get(position).getType() == 1) {
            return VIEW_TYPE_HEADER;
        } else {
            return VIEW_TYPE_BODY;
        }

//        if (position == 0) {
//            return VIEW_TYPE_HEADER;
//        } else {
//            return VIEW_TYPE_BODY;
//        }
    }

    @Override
    public int getItemCount() {
        return orderList.size();
    }

    class WalletHeaderHolder extends RecyclerView.ViewHolder {
        ConstraintLayout clHeader;
        TextView tvTitle, tvAddress, tvNum, tvUnit, tvOut, tvList, tvIn;
        ImageView ivWallet, ivCopy, ivAddAsset, ivCoin, ivWalletStatus;
        GifImageView ivPWRCoin;
        Button btnOneClickSend;
        ConstraintLayout cl_coin_info;
        TextView tv_title,tv_name,tv_price,tv_num,tv_sub_price;
        Button btn_change_rate;

        private boolean isCoinClickable = true;
        public WalletHeaderHolder(View itemView) {
            super(itemView);
            clHeader = itemView.findViewById(R.id.cl_header);
            tvTitle = itemView.findViewById(R.id.tv_account_name);
            tvAddress = itemView.findViewById(R.id.tv_wallet_address);
            tvUnit = itemView.findViewById(R.id.tv_assets_unit);
            tvNum = itemView.findViewById(R.id.tv_assets);
            ivWallet = itemView.findViewById(R.id.iv_wallet);
            ivCopy = itemView.findViewById(R.id.iv_copy);
            ivAddAsset = itemView.findViewById(R.id.iv_add_asset);
            ivCoin = itemView.findViewById(R.id.iv_coin);
            ivPWRCoin = itemView.findViewById(R.id.iv_pwr_coin);
            tvOut = itemView.findViewById(R.id.tv_trade_out);
            tvIn = itemView.findViewById(R.id.tv_trade_in);
            tvList = itemView.findViewById(R.id.tv_trade_list);
            ivWalletStatus = itemView.findViewById(R.id.iv_wallet_status);
            btnOneClickSend = itemView.findViewById(R.id.btn_one_click_send);
            cl_coin_info = itemView.findViewById(R.id.cl_coin_info);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_price = itemView.findViewById(R.id.tv_price);
            tv_num = itemView.findViewById(R.id.tv_num);
            tv_sub_price = itemView.findViewById(R.id.tv_sub_price);
            btn_change_rate = itemView.findViewById(R.id.btn_change_rate);
        }
    }

    class WalletAssetHolder extends RecyclerView.ViewHolder {
        TextView tvAssetName, tvAssetValue, tvAssetExchangeValue;
        CircleImageView ivWallet;

        public WalletAssetHolder(View itemView) {
            super(itemView);
            tvAssetName = itemView.findViewById(R.id.tv_asset_name);
            tvAssetValue = itemView.findViewById(R.id.tv_asset_value);
//            tvAssetExchangeValue = itemView.findViewById(R.id.tv_exchange_value);
            ivWallet = itemView.findViewById(R.id.iv_wallet);
        }
    }

    class WalletFooterHolder extends RecyclerView.ViewHolder {
        //        TextView tvTotalNum, tvTotalPrice;
//        TextView tvRebuy, tvTrace, tvConfirm, tvCheckout;
        public WalletFooterHolder(View itemView) {
            super(itemView);
//            tvTotalNum = itemView.findViewById(R.id.tv_total_num);
//            tvTotalPrice = itemView.findViewById(R.id.tv_total);
//            tvRebuy = itemView.findViewById(R.id.tv_rebuy);
//            tvTrace = itemView.findViewById(R.id.tv_trace);
//            tvConfirm = itemView.findViewById(R.id.tv_confirm);
//            tvCheckout = itemView.findViewById(R.id.tv_checkout);
        }
    }

    /**
     * 复制地址
     *
     * @param address 地址
     */
    private void copyAddress(String address) {
        //复制账号
        ClipboardManager cm = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        cm.setText(address);
        ToastUtils.setBgColor(mContext.getResources().getColor(R.color.black));
        ToastUtils.setMsgColor(mContext.getResources().getColor(R.color.op_text_white_color));
        ToastUtils.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 150);
        ToastUtils.showShort(ResStringUtil.getString(R.string.open_planet_asset_child_wallet_list_copied_clipboard));
    }

    public void setData(String balance) {
        this.balance = balance;
        this.notifyItemChanged(0);
        this.notifyItemChanged(1);
    }
    public void setOptionMarketData(KlineOptionalOrMVModel marketMenuBean){
        klineOptionalOrMVModel = marketMenuBean;
        notifyDataSetChanged();
    }
}
